/*
 * RenameLinksAction.java
 *
 * Created on January 13, 2007, 7:29 PM
 *
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.admin.actions;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.fpdev.core.data.LegacyDB;

import org.fpdev.core.basenet.BLink;
import org.fpdev.core.data.SpatialDataConnection.LinkAttr;
import org.fpdev.apps.admin.AdminClient;

/**
 *
 * @author demory
 */
public class RenameLinksAction implements ACAction {

  private Map<BLink, String> links_; // maps links to original name
  private String newName_;

  /** Creates a new instance of CUpdateAddressRangeAction */
  public RenameLinksAction(Set<BLink> links, String newName) {
    links_ = new HashMap<BLink, String>();
    for (Iterator<BLink> linkIter = links.iterator(); linkIter.hasNext();) {
      BLink link = linkIter.next();
      links_.put(link, link.getDisplayName());
    }
    newName_ = newName;
  }

  public String getName() {
    return "Rename Links";
  }

  public boolean doAction(AdminClient av) {

    for (Iterator<BLink> linkIter = links_.keySet().iterator(); linkIter.hasNext();) {
      linkIter.next().setName(newName_);
    }

    av.getEngine().getDataPackage().getSpatialDataConn().modifyLinks(links_.keySet(), new LinkAttr[] { LinkAttr.NAME});
    //av.getEngine().getDB().updateLinks(links_.keySet().iterator());
    return true;
  }

  public boolean undoAction(AdminClient av) {
    for (Iterator<BLink> linkIter = links_.keySet().iterator(); linkIter.hasNext();) {
      BLink link = linkIter.next();
      link.setName(links_.get(link));
    }

    av.getEngine().getDataPackage().getSpatialDataConn().modifyLinks(links_.keySet(), new LinkAttr[] { LinkAttr.NAME});
    //av.getEngine().getDB().updateLinks(links_.keySet().iterator());
    return true;
  }
}
